package edu.avans.hartigehap.domain;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.beans.factory.annotation.Autowired;

import edu.avans.hartigehap.repository.WebStateRepository;



@Entity
@Table(name = "WEBORDERS")
public class WebOrder implements Serializable {

	private static final long serialVersionUID = 1L;
	private Long id;
	private Collection<WebOrderItem> orderItems = new ArrayList<WebOrderItem>();
	private WebDiscountInterface webdiscount;

	private WebState orderedState;
	private WebState inProgressState;
	private WebState conceptState;
	private WebState finishedState;

	private WebState state;

	public WebOrder() {

		//		finishedState = new FinishedState(this);
		//		orderedState = new OrderedState(this);
		//		inProgressState = new InProgressState(this);
		//		conceptState = new ConceptState(this);
		//		
		//		state = conceptState;

		//		stateRepo.save(finishedState);
		//		stateRepo.save(orderedState);
		//		stateRepo.save(inProgressState);
		//		stateRepo.save(conceptState);
		//		stateRepo.save(state);

	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "WEBORDER_ID")
	public Long getId() {
		return id;
	}	

	public void setId(Long orderid) {
		this.id = orderid;
	}

	@OneToMany(cascade = javax.persistence.CascadeType.ALL)
	@JoinColumn(name = "A4_WEBORDERITEM_WEBORDER_ID", referencedColumnName = "WEBORDER_ID")
	public Collection<WebOrderItem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(Collection<WebOrderItem> orderItems) {
		this.orderItems = orderItems;
	}

	public WebDish addOrderItem(WebDish dish) {
		Iterator<WebOrderItem> orderItemIterator = orderItems.iterator();
		boolean found = false;
		while (orderItemIterator.hasNext()) {
			WebOrderItem orderItem = orderItemIterator.next();
			if (orderItem.getWebDish().getTotalName().equals(dish.getTotalName())) {
				orderItem.incrementQuantity();
				found = true;
				return orderItem.getWebDish();
			}
		}
		if (!found) {
			WebOrderItem orderItem = new WebOrderItem(dish, 1);
			orderItems.add(orderItem);
			return dish;
		}
		return dish;
	}

	public void deleteOrderItem(WebDish dish) {
		Iterator<WebOrderItem> orderItemIterator = orderItems.iterator();
		boolean found = false;
		while (orderItemIterator.hasNext()) {
			WebOrderItem orderItem = orderItemIterator.next();
			if (orderItem.getWebDish().equals(dish)) {
				found = true;
				if (orderItem.getQuantity() > 1) {
					orderItem.decrementQuantity();
				} else { // orderItem.getQuantity() == 1
					orderItemIterator.remove();
				}
				break;
			}
		}
		if (!found) {
			// do nothing
		}
	}

	@Transient
	public BigDecimal getPrice() {

		BigDecimal price = BigDecimal.ZERO;

		Iterator<WebOrderItem> orderItemIterator = orderItems.iterator();
		while (orderItemIterator.hasNext()) {

			price = orderItemIterator.next().getPrice().add(price);
		}
		if (webdiscount != null)
		{
			price = webdiscount.getFinalPrice(price); 
		}
		else
		{
			System.out.println("test");
		}
		return price;
	}

	@Transient
	public BigDecimal getPriceNoDiscount() {
		BigDecimal price = BigDecimal.ZERO;
		Iterator<WebOrderItem> orderItemIterator = orderItems.iterator();
		while (orderItemIterator.hasNext()) {

			price = orderItemIterator.next().getPrice().add(price);
		}
		return price;
	}

	@Transient
	public BigDecimal getDiscount() {
		BigDecimal price = getPriceNoDiscount();

		BigDecimal priceWithDiscount = BigDecimal.ZERO;
		if (webdiscount != null)
		{
			priceWithDiscount = webdiscount.getFinalPrice(price); 
		}
		return price.subtract(priceWithDiscount);
	}

	@OneToOne
	@JoinColumn(name = "DISCOUNT_ID", nullable = true)
	public WebDiscountInterface getWebDiscountInterface() {
		return webdiscount;
	}

	public void setWebDiscountInterface(WebDiscountInterface webdiscount) {
		this.webdiscount = webdiscount;
	}	

	@Transient
	public WebState getWebState(){
		return state;
	}

	public void setWebState(WebState state){
		this.state = state;
	}

	public void setInprogressState() {
		state = inProgressState;
	}

	public void setOrderedState() {
		state = orderedState;
	}

	public void setConceptState() {
		state = conceptState;
	}

	public void setFinishedState() {
		state = finishedState;
	}

	public void changeState() {
		//state.changeState();
		//System.out.println(state.getMessage());
	}

	@Transient
	public WebState getOrderedState() {
		return orderedState;
	}

	public void setOrderedState(WebState orderedState) {
		this.orderedState = orderedState;
	}

	@Transient
	public WebState getInProgressState() {
		return inProgressState;
	}

	public void setInProgressState(WebState inProgressState) {
		this.inProgressState = inProgressState;
	}

	@Transient
	public WebState getConceptState() {
		return conceptState;
	}

	public void setConceptState(WebState conceptState) {
		this.conceptState = conceptState;
	}

	@Transient
	public WebState getFinishedState() {
		return finishedState;
	}

	public void setFinishedState(WebState finishedState) {
		this.finishedState = finishedState;
	}
}
